//
// Created by Administrator on 2021/10/30.
//
#include "DArray.h"
#include "queue"

//using namespace std;
DArray::DArray()
{
    capacity = CAP;
    numOfElements = 0;
    a = new int[capacity];
}

DArray::DArray(int newCapacity)
{
    capacity = newCapacity;
    a = new int[capacity];
    numOfElements = 0;
}

void DArray::addElement(int newElement)
{
    a[numOfElements] = newElement;
    ++numOfElements;
}

int DArray::getNumOfElements() const
{
    return numOfElements;
}

void DArray::replaceElementAt(int newElement, int idx)
{
    a[idx] = newElement;
}

bool DArray::compareArrays(const DArray& otherArray) const
{
    if (numOfElements != otherArray.numOfElements)
        return false;
    else
    {
        int idx = 0;
        while (idx < numOfElements)
        {
            if (a[idx] == otherArray.a[idx])
                ++idx;
            else
                return false;
        }
        return true;
    }
}

void DArray::printArray() const
{
    for (int i = 0; i < numOfElements; ++i)
        cout << a[i] << " ";
    cout << endl;
}

DArray::~DArray( )
{
    delete [] a;
    a = nullptr;
}
bool DArray::search(int searchValue, int& comparisonTimes) const {
    int start = 0, end = this->numOfElements-1;
    while (end >=  start)
    {
        comparisonTimes ++;
        int middle = (start + end) / 2;
        cout << start << " " << middle << " " << end << endl;
        if (this->a[middle] == searchValue)
            return true;
        else
        {
            if (start == end){
                break;
            }
            if (searchValue <  this->a[middle])
                end = middle;
            else
                start = middle + 1;
        }
    }
    return false;
}

void DArray::function55(std::queue<int> &aQueue, DArray& array) const {
    int numOfThis = this->numOfElements;
    int numOfArray = array.numOfElements;
    int constNumOfArray = numOfArray;
    while (numOfThis > 0 || numOfArray > 0) {
        aQueue.push(this->a[this->numOfElements - numOfThis]);
        aQueue.push(array.a[constNumOfArray - numOfArray]);
        array.a[constNumOfArray + (constNumOfArray - numOfArray)]
            = this->a[this->numOfElements - numOfThis];
        array.numOfElements++;
        numOfThis--;
        numOfArray--;
    }
}


